<?php

namespace App\Containers\AppSection\User\UI\API\Transformers;

use App\Containers\AppSection\Authorization\UI\API\Transformers\RoleTransformer;
use App\Containers\AppSection\Ban\Traits\HasBansTransformer;
use App\Containers\AppSection\Profile\UI\API\Transformers\ProfileTransformer;
use App\Containers\AppSection\Status\UI\API\Transformers\StatusTransformer;
use App\Containers\AppSection\User\Models\User;
use App\Ship\Parents\Transformers\Transformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;

class UserTransformer extends Transformer
{
    use HasBansTransformer;

    protected array $availableIncludes = [
        'roles',
        'status',
        'profiles',
        'ban',
        'bans',
    ];

    public function transform(User $t): array
    {
        return [
            'object' => $t->getResourceKey(),
            'include' => $this->getAvailableIncludes(),
            'id' => $t->getHashedKey(),
            'fio' => $t->fio,
            'email' => $t->email,
            'phone' => $t->phone,
            'other_information' => $t->other_information,
            'isBanned' => $t->isBanned(),
            'email_verified_at' => $this->dateFormatter($t->email_verified_at),
            'created_at' => $this->dateFormatter($t->created_at),
            'updated_at' => $this->dateFormatter($t->updated_at),
            'readable_created_at' => $t->created_at->diffForHumans(),
            'readable_updated_at' => $t->updated_at->diffForHumans()];
    }

    public function includeRoles(User $t): Collection
    {
        return $this->collection($t->roles, new RoleTransformer());
    }

    public function includeProfiles(User $t): Collection
    {
        return $this->collection($t->profiles, new ProfileTransformer());
    }

    public function includeStatus(User $t): Item
    {
        return $this->item($t->status, new StatusTransformer());
    }
}
